var WxParse = require("../../lib/wxParse/wxParse.js")
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var Base64util = require("../../utils/base64.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number: 1,
    date: util.formatDay(new Date()),
    start: util.formatDay(new Date()),
    customerInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      ticket: { id: options.id }
    });
    that.getData();
    app.init(options, that);
  },
  getData: function () {
    let that = this;
    util.request(api.SceneTicketDetail, { id: that.data.ticket.id }).then(function (res) {
      if (res.code === '200') {
        var total = 0;
        total = that.data.number * parseFloat(res.datas.ticket.price);
        that.setData({
          ticket: res.datas.ticket,
          total: total
        });

        wx.setNavigationBarTitle({
          //页面标题为路由参数
          title: res.datas.ticket.ticketName
        })
        WxParse.wxParse('content', 'html', Base64util.Base64.decode(res.datas.ticket.info, "utf-8"), that, 5);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getShareInfo(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  fnAddNum: function () {
    var that = this;
    var number = that.data.number + 1;
    var total = that.data.ticket.price * number;
    that.setData({
      number: number,
      total: total
    })
  },
  fnMinNum: function () {
    var that = this;
    if (that.data.number < 2) {
      that.setData({
        number: 1,
        total: that.data.ticket.price
      })
      return;
    }
    var number = that.data.number - 1;
    var total = that.data.ticket.price * number;
    that.setData({
      number: number,
      total: total
    })
  },
  bindDateChange: function (e) {
    var that = this;
    var value = e.detail.value;
    that.setData({
      date: value
    })
  },
  fnOrder: function () {
    var ticket = this.data.ticket;
    var type = "scene";
    var id = ticket.id;
    var url = "/pages/preOrder/sceneOrder/scenorder?type=" + type +
      "&id=" + id;
    wx.navigateTo({
      url: url,
    });

  },
  fnInput: function (e) {
    var that = this;
    var customerInfo = that.data.customerInfo;
    customerInfo[e.currentTarget.id] = e.detail.value;
    that.setData({
      customerInfo: customerInfo
    })
  },
  fnToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  gotoHome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})